package com.project.internship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.internship.entity.Major;
import com.project.internship.mapper.MajorMapper;
import com.project.internship.service.MajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-04-07
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Autowired
    MajorMapper majorMapper;

    @Override
    public List<Major> getMajors() {
        return majorMapper.getMajors(UserUtils.getCurrentUser().getDepartmentId());
    }

    @Override
    public Integer addMajor(Major major) {
        major.setDepartmentId(UserUtils.getCurrentUser().getDepartmentId());
        return majorMapper.insert(major);
    }

    @Override
    public Integer deleteMajor(Major major) {
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        wrapper.select("id")
                .eq("department_id",UserUtils.getCurrentUser().getDepartmentId())
                .eq("name",major.getName());
        Major major1 = majorMapper.selectOne(wrapper);
        return majorMapper.deleteById(major1.getId());
    }
}
